package com.work.service.main.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.qiniu.util.Auth;
import com.work.constant.ExceptionDescription;
import com.work.constant.TallyType;
import com.work.dto.main.RetreatSetAppSubmitDTO;
import com.work.dto.main.RetreatSetSearchDTO;
import com.work.entity.main.RetreatSet;
import com.work.mapper.main.RetreatSetMapper;
import com.work.service.main.IRetreatSetService;
import com.work.service.main.ITShipBillService;
import com.work.service.main.ITShipBillStatusService;
import com.work.vo.main.*;
import lombok.AllArgsConstructor;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.Func;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 退关设置接口实现
 *
 * @author ：dujianyu
 * @since ：Created in 2024/5/27 13:25
 */
@Service
@AllArgsConstructor
public class RetreatSetServiceImpl extends BaseServiceImpl<RetreatSetMapper, RetreatSet> implements IRetreatSetService {

    private ITShipBillService itShipBillService;
    private ITShipBillStatusService itShipBillStatusService;

    @Override
    public boolean submitRetreatSet(RetreatSetAppSubmitDTO dto) {
        if (Func.isNull(dto.getId()) && !Func.isNull(dto.getRetreatStorageId())) {
            if (isStorageAlreadyRetreat(dto.getRetreatStorageId())) {
                throw new RuntimeException(ExceptionDescription.LOGIC_ERROR + "该垛已经设置退关量");
            }
        }


        RetreatSet entity = BeanUtil.copy(dto, RetreatSet.class);

        if (Func.isNull(entity)) {
            throw new RuntimeException(ExceptionDescription.FUN_ERROR + "submitOneStockTally-BeanUtil.copy复制失败");
        }

        if (Func.isNull(entity.getTShipBillId())) {
            Long newTshipBillId = itShipBillService.addTransportBill(entity.getTransportShipId(), entity.getTransportBillId());
            entity.setTShipBillId(newTshipBillId);
        }

        if (!Func.isNull(dto.getId())) {
            RetreatSet oldRetreatSet = baseMapper.selectById(entity.getId());
            itShipBillStatusService.changePieceTon(TallyType.RETREAT, TallyType.SUB, entity.getTShipBillId(), oldRetreatSet.getRetreatPiece(), oldRetreatSet.getRetreatTon());
        } else {
            entity.setCreateUserName(AuthUtil.getNickName());
        }


        entity.setUpdateUserName(AuthUtil.getNickName());

        return itShipBillStatusService.changePieceTon(TallyType.RETREAT, TallyType.ADD, entity.getTShipBillId(), dto.getRetreatPiece(), dto.getRetreatTon())
                && super.saveOrUpdate(entity);
    }

    @Override
    public boolean removeRetreatSet(Long id) {
        RetreatSet retreatSet = baseMapper.selectById(id);
        return itShipBillStatusService.changePieceTon(TallyType.RETREAT, TallyType.SUB, retreatSet.getTShipBillId(), retreatSet.getRetreatPiece(), retreatSet.getRetreatTon())
                && super.deleteLogic(Func.toLongList(id.toString()));
    }

    @Override
    public IPage<RetreatSetVO> selectRetreatSetPage(IPage<RetreatSetVO> page, RetreatSetSearchDTO dto) {
        if("Y".equals(dto.getPersonal())) {
            dto.setCreateUser(AuthUtil.getUserId());
        }
        List<RetreatSetVO> data = baseMapper.selectRetreatSetList(dto, page);
        return page.setRecords(data);
    }

    @Override
    public List<RetreatTallyVO> selectRetreatTallyList(RetreatSetSearchDTO dto) {
        return baseMapper.selectRetreatTallyList(dto);
    }

    @Override
    public RetreatTotalVO selectRetreatTotal(RetreatSetSearchDTO dto) {
        return baseMapper.selectRetreatDataTotal(dto);
    }

    @Override
    public IPage<RetreatSetStatVO> selectRetreatSetStatPage(IPage<RetreatSetStatVO> page, RetreatSetSearchDTO dto) {
        List<RetreatSetStatVO> data = baseMapper.selectRetreatSetStatList(dto, page);
        return page.setRecords(data);
    }

    @Override
    public RetreatTotalVO selectRetreatSetStatTotal(RetreatSetSearchDTO dto) {
        return baseMapper.selectRetreatSetStatTotal(dto);
    }

    @Override
    public IPage<RetreatSetStatDetailVO> selectRetreatSetStatDetailPage(IPage<RetreatSetStatDetailVO> page, RetreatSetSearchDTO dto) {
        List<RetreatSetStatDetailVO> data = baseMapper.selectRetreatSetStatDetailList(dto, page);
        return page.setRecords(data);
    }

    @Override
    public IPage<RetreatSetStatTallyVO> selectRetreatSetStatTallyPage(IPage<RetreatSetStatTallyVO> page, RetreatSetSearchDTO dto) {
        List<RetreatSetStatTallyVO> data = baseMapper.selectRetreatSetStatTallyList(dto, page);
        return page.setRecords(data);
    }

    private Boolean isStorageAlreadyRetreat(Long storageId) {
        QueryWrapper<RetreatSet> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().select(RetreatSet::getId).eq(RetreatSet::getIsDeleted, 0).eq(RetreatSet::getRetreatStorageId, storageId);
        List<RetreatSet> list = baseMapper.selectList(queryWrapper);
        return !Func.isNull(list) && !list.isEmpty();
    }
}
